﻿using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Subscription;
using GraphQL.Types;
using NetCore_GraphQL.Entity;
using NetCore_GraphQL.GraphType;
using NetCore_GraphQL.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetCore_GraphQL
{
    public class RootSubscriber:ObjectGraphType
    {
        private readonly SysInfoService _sysInfoService = new SysInfoService();
        public RootSubscriber()
        {
            Name = "rootSubscrober";
            Description = "消息订阅";
            AddField(new EventStreamFieldType
            {
                Name = "messageAdded",
                Type = typeof(SysInfoType),
                Resolver = new FuncFieldResolver<SysInfo>(ResolveMessage),
                Subscriber = new EventStreamResolver<SysInfo>(Subscribe)
            });
        }
        private SysInfo ResolveMessage(IResolveFieldContext context)
        {
            return context.Source as SysInfo;
        }

        private IObservable<SysInfo> Subscribe(IResolveEventStreamContext context)
        {
            return _sysInfoService.GetSysInfos();
        }
    }
}
